package com.ostrich.a2104shopping.ui.home;

import android.os.Bundle;
import android.view.View;
import android.widget.SearchView;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.ostrich.a2104shopping.R;
import com.ostrich.a2104shopping.databinding.FragmentHomeBinding;
import com.ostrich.a2104shopping.ui.home.adapter.CategoryAdapter;
import com.ostrich.a2104shopping.ui.home.adapter.GoodsAdapter;
import com.ostrich.a2104shopping.ui.home.entity.CategoryEntity;
import com.ostrich.a2104shopping.ui.home.entity.GoodsEntity;
import com.ostrich.a2104shopping.ui.home.viewmodel.HomeViewModel;
import com.ostrich.lib_common.base.BaseMvvmFragment;
import com.ostrich.lib_common.net.BaseResponseEntity;

import java.util.List;
import java.util.concurrent.Executors;

public class HomeFragment extends BaseMvvmFragment<HomeViewModel,FragmentHomeBinding> {
    private CategoryAdapter mCategoryAdapter;
    private GoodsAdapter mGoodsAdapter;
    private int page = 1;

    @Override
    public int bindLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        mCategoryAdapter = new CategoryAdapter();
        binding.rvCategory.setAdapter(mCategoryAdapter);
        binding.rvCategory.setLayoutManager(new LinearLayoutManager(getContext()));
        mGoodsAdapter = new GoodsAdapter();
        binding.rvGoods.setAdapter(mGoodsAdapter);
        binding.rvGoods.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Executors.newCachedThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        List<GoodsEntity> list = viewModel.select(newText);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mGoodsAdapter.refreshData(list);
                            }
                        });

                    }
                });
                return true;
            }
        });
    }

    @Override
    public void initData() {
        //分类
        viewModel.category().observe(this, new Observer<BaseResponseEntity<List<CategoryEntity>>>() {
            @Override
            public void onChanged(BaseResponseEntity<List<CategoryEntity>> listBaseResponseEntity) {
                if(listBaseResponseEntity.getCode() == 200){
                    List<CategoryEntity> data = listBaseResponseEntity.getData();
                    mCategoryAdapter.refreshData(data);

                }
            }
        });
        //商品
        viewModel.info(0,page,10).observe(this, new Observer<BaseResponseEntity<List<GoodsEntity>>>() {
            @Override
            public void onChanged(BaseResponseEntity<List<GoodsEntity>> listBaseResponseEntity) {
                if(listBaseResponseEntity.getCode() == 200){
                    List<GoodsEntity> data = listBaseResponseEntity.getData();
                    mGoodsAdapter.refreshData(data);

                    //存数据
                    Executors.newCachedThreadPool().execute(new Runnable() {
                        @Override
                        public void run() {
                            viewModel.insertAll(data);
                        }
                    });



                }
            }
        });
        //item点击
        mGoodsAdapter.setItemClickListener(new GoodsAdapter.ItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("id",mGoodsAdapter.getDataByPosition(position).getId());
                startActivityBundle(GoodsDetailActivity.class,bundle);
            }
        });

    }

    @Override
    public void initListener() {

    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(getActivity()).get(HomeViewModel.class);
    }
}